
const app = getApp();
var api = require("../../api/api.js");
var event = require("../../event/event.js");
var utils = require("../../utils/utils.js");
var storageManage = require("../../manage/storageManage.js");
var config = require("../../config/config.js");
var isbind = true;
Page({
  data: {
    user_type: storageManage.getGlobalConfig().user_type,
    status: '',
    mobile_number: '',
    user_count: '',
    order_count: {
      "cost": 0.0,
      "count": 0,
      "price": 0
    },
    showClear: false//是否显示清除缓存
  },

  onLoad: function () {
    wx.showLoading()
    var that = this;
    event.on('user', this, function (data) {
      that.updata();
    });

    api.default.check(function () {
      if (that.data.user_type != 0 || that.data.user_type != 1) {
        api.default.api('get_conf', {}, function (resD) {
          storageManage.setGlobalConfig(resD.data);
          that.setData({
            user_type: storageManage.getGlobalConfig().user_type
          })
        })
      }
      var info = storageManage.getInfo();
      console.log(info)
      that.setData({
        userName: info.nickName,
        headIcon: info.avatarUrl
      });
      that.updata();

    });
    if (config.serverUrl.indexOf('dev.api.miaorain.com') > 0) {
      this.setData({
        showClear: false
      })
    } else {
      this.setData({
        showClear: true
      })
    }


  },
  /**
   * 
   * updata data
   */
  updata: function () {
    api.default.api('me', {}, this.successCallback, this.errorCallback);
  },

  //me
  successCallback: function (data) {
    wx.hideLoading()
    api.default.api('get_user', {}, this.userSuccessCallback, this.userErrorCallback);

    this.setData({
      nPayNum: data.data.pending_pay_order,
      nShipNum: data.data.pending_send_order,
      nHarvestNum: data.data.pending_recv_order,
      mobile_number: data.data.mobile_number
    });
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新

  },

  errorCallback: function (e, du) {
    console.log(e)
    wx.hideLoading()
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },

  //getUser
  userSuccessCallback: function (data) {
    console.log(data)
    this.setData({
      user_id: data.data.user_id,
      user_count: data.data.user_count,
      order_count: data.data.order_count
    });
  },

  userErrorCallback: function (e) {
    console.log(e)
  },

  toMonthOrder: utils.throttle(function (e) {
    api.default.send_template(e)
    wx.navigateTo({
      url: '../monthOrder/monthOrder'
    });
  }),
  onWallet: utils.throttle(function (e) {
    api.default.send_template(e)
    wx.navigateTo({
      url: '../wallet/wallet'
    });
  }),
  //订单列表页
  onOrderClick: utils.throttle(function (e) {
    api.default.send_template(e)
    wx.navigateTo({
      url: '../order/order?id=' + e.currentTarget.id
    });
  }),
  //我的客户
  onMyClient: utils.throttle(function (e) {
    api.default.send_template(e)
    wx.navigateTo({
      url: '../myClient/myClient'
    })
  }),

  //退换 售后
  toReturns: utils.throttle(function (e) {
    api.default.send_template(e)
    wx.navigateTo({
      url: '../returns/returns'
    });
  }),
  //地址管理
  onManageAddrClick: utils.throttle(function () {
    wx.navigateTo({
      url: '../address/addr_list/addr_list',
    });
  }),
  //身份识别码
  toIdentity: utils.throttle(function () {
    wx.navigateTo({
      url: '../identity/identity',
    });
  }),

  //反馈
  toFeedback: utils.throttle(function () {
    wx.navigateTo({
      url: '../feedback/feedback',
    });
  }),
  //清除缓存
  onCLearClick: utils.throttle(function () {
    try {
      wx.clearStorageSync();
      wx.showToast({
        title: '清除成功',
      })
    } catch (e) {
      // Do something when catch error
    }
  }),
  //下拉刷新
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.updata();
  }
})